import { Controller } from "../../mvc/core/Controller";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SCountryPrivilegeInfo } from "../../../Message/Game/SCountryPrivilegeInfo";
import { ECountryPrivilege } from "../../../Message/Game/ECountryPrivilege";
import { DataEvent } from "../events/DataEvent";
import { GameConst } from "../../component/gconst/GameConst";
import { ClockManager } from "../manager/ClockManager";
import { SkillInfo } from "../model/SkillInfo";
import { Alert } from "../../../com/mui/controls/Alert";
import { Language } from "../../../extend/language/Language";
import { GameProxy } from "../mvc/GameProxy";
import { TSkill } from "../../../Message/Db/Tables/TSkill";
import { ECountryPosition } from "../../../Message/Game/ECountryPosition";
import { CountryBuffSkillId } from "../../../GameConst/CountryBuffSkillId";
import { CountryCallTogetherKillId } from "../../../GameConst/CountryCallTogetherKillId";
type int = number;
//class CountryBuffController
    
    export  class CountryBuffController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCountryPrivilegeInfo, this.buffSkilInfoHandler);
            Dispatcher.addEventListener(EventName.CountryBuffSkillOpened, this.openHandler);
            Dispatcher.addEventListener(EventName.CountryBuffSkillClose, this.closeHandler);
            Dispatcher.addEventListener(EventName.CountrySelPositionGot, this.gotMyPositionHandler);
            Dispatcher.addEventListener(EventName.SkillUseSkill, this.useSkillReqHadler);
            Dispatcher.addEventListener(EventName.CountryPositionUpdate, this.countryPositionUpdateHandler);
            return;
        }

        /* internal  */buffSkilInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SCountryPrivilegeInfo;
            if (loc1 == null) 
            {
                return;
            }
            var loc2=loc1.privilegeType;
            switch (loc2) 
            {
                case ECountryPrivilege._ECountryPrivilegeOpenCountryBuff:
                {
                    cache.countryBuffSkill.buffKillCDInfo = loc1;
                    this.doBuffCdHandler();
                    break;
                }
                case ECountryPrivilege._ECountryPrivilegeCallTogether:
                {
                    cache.countryBuffSkill.callTogetherCDInfo = loc1;
                    this.doCallTogetherCdHandler();
                    break;
                }
            }
            return;
        }

        /* internal  */doBuffCdHandler(): void
        {
            var loc1=cache.countryBuffSkill.buffKillCDInfo;
            if (loc1 == null) 
            {
                return;
            }
            cache.timeCdCache.updateCountryBuffCD(loc1);
            if (this._isIconShowing) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.UpdateBuffSkillCDReq, {"skillId": CountryBuffSkillId, "cdItemData": cache.timeCdCache.countryBuffCD}));
            }
            return;
        }

        /* internal  */doCallTogetherCdHandler(): void
        {
            var loc1=cache.countryBuffSkill.callTogetherCDInfo;
            if (loc1 == null) 
            {
                return;
            }
            var loc2=ClockManager.instance.nowDate.time - loc1.useTimes * 1000;
            cache.timeCdCache.updateCountryCallTogettherCd(flash.utils.getTimer() - loc2, 600000);
            if (this._isCallTogetherShowing) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.UpdateBuffSkillCDReq, {"skillId": CountryCallTogetherKillId, "cdItemData": cache.timeCdCache.countryCallTogettherCd}));
            }
            return;
        }

        /* internal  */gotMyPositionHandler(arg1: DataEvent): void
        {
            this.checkAndShowIcon();
            this.checkAndShowCallTogether();
            return;
        }

        /* internal  */countryPositionUpdateHandler(arg1: DataEvent): void
        {
            if (this._isIconShowing) 
            {
                if (!cache.country.doIHavePrivilege(ECountryPrivilege._ECountryPrivilegeOpenCountryBuff)) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.RemoveBuffSkillByReq, cache.countryBuffSkill.curSkillInfo));
                    cache.countryBuffSkill.curSkillInfo = null;
                    this._isIconShowing = false;
                }
            }
            if (this._isCallTogetherShowing) 
            {
                if (!cache.country.doIHavePrivilege(ECountryPrivilege._ECountryPrivilegeCallTogether)) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.RemoveBuffSkillByReq, cache.countryBuffSkill.curCallTogetherInfo));
                    cache.countryBuffSkill.curCallTogetherInfo = null;
                    this._isCallTogetherShowing = false;
                }
            }
            this.checkAndShowIcon();
            this.checkAndShowCallTogether();
            return;
        }

        /* internal  */useSkillReqHadler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SkillInfo;
            if (loc1 == null) 
            {
                return;
            }
            var loc2=loc1.skillId;
            switch (loc2) 
            {
                case GameConst.CountryBuffSkillId:
                {
                    Alert.show(Language.getString(80429), null, Alert.OK | Alert.CANCEL, null, this.useBuffCloseHandler);
                    break;
                }
                case GameConst.CountryCallTogetherKillId:
                {
                    Alert.show(Language.getString(80473), null, Alert.OK | Alert.CANCEL, null, this.togetherCloseHandler);
                    break;
                }
            }
            return;
        }

        /* internal  */useBuffCloseHandler(arg1: int): void
        {
            if (arg1 != Alert.OK) 
            {
                return;
            }
            GameProxy.country.openCountryBuff();
            return;
        }

        /* internal  */togetherCloseHandler(arg1: int): void
        {
            if (arg1 != Alert.OK) 
            {
                return;
            }
            GameProxy.country.callTogether();
            return;
        }

        /* internal  */openHandler(arg1: Object): void
        {
            cache.countryBuffSkill.isCanOpenBuffSkill = true;
            this.checkAndShowIcon();
            this.checkAndShowCallTogether();
            return;
        }

        /* internal  */checkAndShowIcon(): void
        {
            if (!cache.countryBuffSkill.isCanOpenBuffSkill) 
            {
                return;
            }
            if (this._isIconShowing) 
            {
                return;
            }
            if (cache.countryBuffSkill.curSkillInfo == null) 
            {
                cache.countryBuffSkill.curSkillInfo = this.skillInfo;
            }
            if (cache.countryBuffSkill.curSkillInfo == null) 
            {
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.AddToBuffSkillByReq, cache.countryBuffSkill.curSkillInfo));
            this._isIconShowing = true;
            this.doBuffCdHandler();
            return;
        }

        /* internal  */checkAndShowCallTogether(): void
        {
            if (!cache.countryBuffSkill.isCanOpenBuffSkill) 
            {
                return;
            }
            if (this._isCallTogetherShowing) 
            {
                return;
            }
            if (!cache.country.doIHavePrivilege(ECountryPrivilege._ECountryPrivilegeCallTogether)) 
            {
                return;
            }
            var loc1=new SkillInfo();
            loc1.tSkill = new TSkill();
            loc1.tSkill.skillIcon = "countryBuffKill_Call";
            loc1.tSkill.skillName = Language.getString(80474);
            loc1.tSkill.skillDescription = Language.getString(80475);
            loc1.tSkill.skillLevel = 1;
            loc1.tSkill.cooldownTime = 600000;
            loc1.tSkill.skillId = GameConst.CountryCallTogetherKillId;
            loc1.tSkill.skillType = GameConst.CountryBuffSkillType;
            loc1.learned = true;
            cache.countryBuffSkill.curCallTogetherInfo = loc1;
            Dispatcher.dispatchEvent(new DataEvent(EventName.AddToBuffSkillByReq, cache.countryBuffSkill.curCallTogetherInfo));
            this._isCallTogetherShowing = true;
            this.doCallTogetherCdHandler();
            return;
        }

        /* internal  */closeHandler(arg1: Object): void
        {
            cache.countryBuffSkill.isCanOpenBuffSkill = false;
            if (this._isIconShowing && !(cache.countryBuffSkill.curSkillInfo == null)) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.RemoveBuffSkillByReq, cache.countryBuffSkill.curSkillInfo));
                cache.countryBuffSkill.curSkillInfo = null;
                this._isIconShowing = false;
            }
            if (this._isCallTogetherShowing && !(cache.countryBuffSkill.curCallTogetherInfo == null)) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.RemoveBuffSkillByReq, cache.countryBuffSkill.curCallTogetherInfo));
                cache.countryBuffSkill.curCallTogetherInfo = null;
                this._isCallTogetherShowing = false;
            }
            return;
        }

        /* internal  */get skillInfo(): SkillInfo
        {
            var loc1=this.getIconName();
            if (loc1 == "") 
            {
                return null;
            }
            var loc2=new SkillInfo();
            loc2.tSkill = new TSkill();
            loc2.tSkill.skillIcon = loc1;
            loc2.tSkill.skillName = Language.getString(80476);
            loc2.tSkill.skillDescription = Language.getString(80477);
            loc2.tSkill.skillLevel = 1;
            loc2.tSkill.cooldownTime = 600000;
            loc2.tSkill.skillId = GameConst.CountryBuffSkillId;
            loc2.tSkill.skillType = GameConst.CountryBuffSkillType;
            loc2.learned = true;
            return loc2;
        }

        /* internal  */getIconName(): string
        {
            var loc1="";
            var loc2=cache.country.selfPosition;
            switch (loc2) 
            {
                case ECountryPosition._ECountryPositionKing:
                {
                    loc1 = "countryBuffKill_King";
                    break;
                }
                case ECountryPosition._ECountryPositionGeneral:
                {
                    loc1 = "countryBuffKill_Master";
                    break;
                }
                case ECountryPosition._ECountryPositionMarquis1:
                {
                    loc1 = "countryBuffKill_Left";
                    break;
                }
                case ECountryPosition._ECountryPositionMarquis2:
                {
                    loc1 = "countryBuffKill_Right";
                    break;
                }
                default:
                {
                    loc1 = "";
                }
            }
            return loc1;
        }

        private /* var */_isIconShowing: Boolean=false;

        private /* var */_isCallTogetherShowing: Boolean=false;

        private /* var */_isFirstCall: Boolean=true;

        private /* var */_isFirstBuff: Boolean=true;
    }
